<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改新闻(文章新闻,视频新闻)')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-news-edit" th:object="${news}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select name="type" id="newsClassType" class="form-control m-b" th:with="type=${@dict.getType('news_type')}" disabled="disabled">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{type}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">新闻分类</label>
                <div class="col-sm-8">
                    <select name="newsClassId" class="form-control m-b" th:with="type=${@dict.getType('news_type')}">
                        <option th:each="dict : ${newsClassListClass}" th:text="${dict.name}" th:value="${dict.id}" th:field="*{newsClassId}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="title" th:field="*{title}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">作者：</label>
                <div class="col-sm-8">
                    <input name="author" th:field="*{author}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group" id="portraitDiv">
                <label class="col-sm-3 control-label">头像：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="portraitUpload">上传头像</button>
                        <div class="layui-upload-list" id="portraitShow">
                            <img th:if="${news.portrait}" th:src="${news.portrait}" style="width: 112px; height: 63px; margin-right: 10px;">
                        </div>
                    </div>
                    <input name="portrait" th:field="*{portrait}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group" id="articleCoversDiv" style="display: none;">
                <label class="col-sm-3 control-label">文章封面：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="aCoversUpload">上传文章封面</button>
                        <div class="layui-upload-list" id="aCoversShow">
                            <th:block th:each="image : ${#strings.listSplit(news.articleCovers, ',')}">
                                <img th:src="${image}" style="width: 112px; height: 112px; margin-right: 10px;">
                            </th:block>
                        </div>
                    </div>
                    <input id="articleCovers" name="articleCovers" th:field="*{articleCovers}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group" id="articleContentDiv" style="display: none;">
                <label class="col-sm-3 control-label">文章内容：</label>
                <div class="col-sm-8">
                    <div class="ibox-content no-padding">
                        <div class="summernote" th:utext="${news.articleContent}">
                        </div>
                    </div>
                    <input  id="articleContent"  name="articleContent" th:field="*{articleContent}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">用户发布新闻内容：</label>
                <div class="col-sm-8">
                    <input name="content" th:field="*{content}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group" style="display: none;">
                <label class="col-sm-3 control-label">发布者：</label>
                <div class="col-sm-8">
                    <input name="publisher" id="publisherNum" th:field="*{publisher}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group" id="videoCoverDiv" style="display: none;">
                <label class="col-sm-3 control-label">视频封面：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="vCoverUpload">上传视频封面</button>
                        <div class="layui-upload-list" id="vCoverShow">
                                <img th:if="${news.videoCover}" th:src="${news.videoCover}" style="width: 112px; height: 63px; margin-right: 10px;">
                        </div>
                    </div>
                    <input name="videoCover" th:field="*{videoCover}" class="form-control" type="hidden">
                </div>
            </div>
<!--            <div class="form-group" id="videoUrlDiv" style="display: none;">-->
<!--                <label class="col-sm-3 control-label">视频url：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="videoUrl" th:field="*{videoUrl}" class="form-control" type="text">-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group" id="videoUrlDiv" style="display: none;">
                <label class="col-sm-3 control-label">视频：</label>
                <div class="col-sm-8">
                    <button type="button" class="layui-btn" id="test1">视频</button>
                    <div class="layui-upload-list">
                        <div id="img_header" style="padding: 5px;"><embed th:src="*{videoUrl}" style='height: 200px;width: 200px'></div>
                        <input type="hidden" id="videoUrl" name="videoUrl" th:value="*{videoUrl}">
                        <p id="demoText1"></p>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">浏览量：</label>
                <div class="col-sm-8">
                    <input name="numSee" th:field="*{numSee}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">点赞量：</label>
                <div class="col-sm-8">
                    <input name="likes" th:field="*{likes}" class="form-control" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">评论量：</label>
                <div class="col-sm-8">
                    <input name="commentNum" th:field="*{commentNum}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" th:field="*{sort}" class="form-control" type="text">
                </div>
            </div>

<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">发布时间：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <div class="input-group date">-->
<!--                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>-->
<!--                        <input name="releaseAt" th:value="${#dates.format(news.releaseAt, 'yyyy-MM-dd HH:mm:ss')}" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript" th:inline="javascript">
        $(document).ready(function () {
            $('.summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $editable) {
                        // console.log('onChange:', contents, $editable);
                        var aHTML = $('.summernote').summernote('code');
                        console.log(aHTML);
                        $("#articleContent").val(aHTML);
                    }
                }
            });
            var ia = $("#newsClassType").val();
            if(ia==0){
                $("#articleCoversDiv").show();
                $("#articleContentDiv").show();
            }
            if(ia==1){
                $("#videoCoverDiv").show();
                $("#videoUrlDiv").show();
            }
        });
        var acNum = 0;
        var prefix = ctx + "jiaqin/news";
        $("#form-news-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-news-edit').serialize());
            }
        }

        $("input[name='releaseAt']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            autoclose: true
        });

        layui.use('upload', function(){
            var $ = layui.jquery
                ,upload = layui.upload;

            //头像上传
            var uploadInst = upload.render({
                elem: '#portraitUpload'
                ,url: '/common/upload' //改成您自己的上传接口
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){

                        $('#portraitShow').html('<img src="'+ result +'" alt="'+ file.name +'" style="width: 112px; height: 63px;">')
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code > 0){
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    console.log(res);
                    $("#portrait").val(res.realPath);
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#vCoverUpload'
                ,url: '/common/upload' //改成您自己的上传接口
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){

                        $('#vCoverShow').html('<img src="'+ result +'" alt="'+ file.name +'" style="width: 112px; height: 63px;">')
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code > 0){
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    console.log(res);
                    $("#videoCover").val(res.realPath);
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

            //多图片上传
            upload.render({
                elem: '#aCoversUpload'
                ,url: '/common/upload'
                ,multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        if(acNum == 0){
                            $('#aCoversShow').html("");
                        }
                        $('#aCoversShow').append('<img src="'+ result +'" alt="'+ file.name +'" style="width: 112px; height: 112px; margin-right: 10px;">')
                    });
                }
                ,done: function(res){
                    //上传完毕
                    console.log(res);
                    if(acNum == 0){
                        $("#articleCovers").val("");
                        $("#articleCovers").val(res.realPath);
                        acNum++;
                    }else{
                        var acUrls = $("#articleCovers").val() + ','+res.realPath;
                        $("#articleCovers").val(acUrls);
                    }
                    console.log($("#articleCovers").val());
                }
            });

        });

        layui.use('upload', function() {
            var $ = layui.jquery
                , upload = layui.upload;

            //视频上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/common/upload' //改成您自己的上传接口
                ,accept: 'file'
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#img_header").html("<embed src='" + res.url + "' style='width:200px;height:200px;'>");
                    $("#videoUrl").val(res.url);
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText1');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
        })
    </script>
</body>
</html>
